#include "TextureState.h"

RenderState::Type TextureState::get_type() const
{
	GL_TEXTURE;
}

TextureState::TextureState()
{
}

TextureState::~TextureState()
{
	remove_all();
}


void TextureState::update( RenderState*& old_state )
{
	assert( old_state == m_default );
	if ( old_state != m_default )
		return;

	for ( unsigned int i = 0; i < MAX_TEXTURES; i++ )
	{
		// save old state for restoring
		Texture* save = m_default->get(i);
		m_default->set( i, m_texture[i] );
		m_texture[i] = save;
	}
}

void TextureState::restore( RenderState* old_state )
{
	assert( old_state == m_default );
	
	for ( unsigned int index = 0; index < MAX_TEXTURES; index++ )
	{
		TextureState* save = m_default->get[index];
		m_default->set( index, m_texture[index] );
		m_texture[index] = save;
	}
}

RenderState* TextureState::copy()
{
	TextureState* state = new TextureState;
	for ( unsigned int index = 0; index < MAX_TEXTURES; i++ )
	{
		state->m_texture[index] = m_texture[index];
	}
	return state;
}

void TextureState::set( unsigned int index, Texture* texture )
{
	if ( index >= MAX_TEXTURES )
		return;
	m_texture[index] = texture;
}

Texture* TextureState::get( unsigned int index )
{
	if( index >= MAX_TEXTURES )
		return 0;
	return m_texture[index];
}

TexturePtr TextureState::remove( unsigned int index )
{
	if ( index > MAX_TEXTURES )
		return 0;
	TexturePtr save = m_texture[index];
	m_texture[index] = 0;
	return save;
}

void TextureState::remove_all()
{
	for ( unsigned int index = 0; index < MAX_TEXTURES; index++ )
		m_texture[index] = 0;
}

unsigned int TextureState::get_num() const
{
	unsigned int num = 0;

	for ( unsigned int i = 0; i < MAX_TEXTURES; i++ )
	{
		if ( m_texture[i] )
			num++;
	}
	return num;
}



